package com.lele.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

@Configuration
public class SecurityConfig{
    @Resource
    private HttpSecurity httpSecurity;
    @Resource
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    @Resource
    private MyauthenticationEntryPoint authenticationEntryPoint;
    @Resource
    private MyaccessDeniedHandler myaccessDeniedHandler;
    @Autowired
    private CustomerDecisionFilter decisionFilter;
    @Autowired
    private CustomerFilter customerFilter;

    @Bean
    //还需要配置这个
    //TODO为什么还需要这个
    public WebSecurityCustomizer webSecurityCustomizer(){
        return web->{
            web.ignoring().antMatchers("/login","/logout","/captcha");
        };
    }


    @Bean
    SecurityFilterChain securityFilterChain() throws Exception {
        httpSecurity.csrf().disable()
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(myaccessDeniedHandler)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                //动态权限配置

                .and().headers().cacheControl();
        httpSecurity.userDetailsService(userDetailsService());
        httpSecurity.authorizeRequests()
                .antMatchers("/login","/logout","/captcha").permitAll()
                .anyRequest().authenticated()
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setAccessDecisionManager(decisionFilter);
                        o.setSecurityMetadataSource(customerFilter);
                        return o;
                    }
                });
        return httpSecurity.build();
    }

    ;

    @Bean
    UserDetailsService userDetailsService() {
        return new MyUserDetailsService();
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
